package com.dao;

import com.entity.book_examine;
import com.entity.chapter_examine;
import com.utils.PropertiesUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class chapter_examineDao {
    public void add(chapter_examine chapter) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "insert into chapter_examine(bookname,author_name,chapter_name,chapter_no,address) values(?,?,?,?,?)";
        queryRunner.update(sql,chapter.getBookname(),chapter.getAuthor_name(),chapter.getChapter_name(),chapter.getChapter_no(),chapter.getAddress());
    }
    public List<chapter_examine> findall() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from chapter_examine ";
        List<chapter_examine> list = queryRunner.query(sql, new BeanListHandler<>(chapter_examine.class));
        return list;
    }

    public List<chapter_examine> findbyauthor(String author) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from chapter_examine where author_name = ?";
        List<chapter_examine> list = queryRunner.query(sql, new BeanListHandler<>(chapter_examine.class),author);
        return list;
    }

    public chapter_examine findbybookname(String bookname) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from chapter_examine where bookname = ?";
        chapter_examine book = queryRunner.query(sql, new BeanHandler<>(chapter_examine.class),bookname);
        return book;
    }
    public void delBybookname(String bookname,String chapter_name) throws SQLException{
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "delete from chapter_examine where bookname = ? and chapter_name = ?";
        queryRunner.update(sql,bookname,chapter_name);
    }
}
